//
//  TMMath.m
//  cutter
//
//  Created by mac on 15/12/31.
//  Copyright © 2015年 e360. All rights reserved.
//

#import "TMMath.h"


//顶点****************************************************************************
//加
CGPoint TMPointAdd(CGPoint a, CGPoint b){
    CGPoint v; v.x = a.x + b.x; v.y = a.y + b.y; return v;
}
//减
CGPoint TMPointSubtract(CGPoint a, CGPoint b){
    CGPoint v; v.x = a.x - b.x; v.y = a.y - b.y; return v;
}
//插值
CGPoint TMPointInterpolate(CGPoint a, CGPoint b, CGFloat s){
    CGPoint v; v.x = a.x + s * (b.x - a.x); v.y = a.y + s * (b.y - a.y); return v;
}
//(a,b,c)的角平分线
CGPoint TMPointBisector(CGPoint a, CGPoint b, CGPoint c){
    CGPoint d = TMPointNormalize(TMPointSubtract(b, a));
    CGPoint e = TMPointNormalize(TMPointSubtract(c, b));
    return TMPointAdd(d, e);
}
//归一化
CGPoint TMPointNormalize(CGPoint v){
    CGFloat l = TMPointLength(v);
    if (l > 0){
        CGFloat s = 1.0 / l;
        v.x *= s;
        v.y *= s;
    }
    return v;
}
//距离
CGFloat TMPointDistance(CGPoint a, CGPoint b){
    CGPoint v = TMPointSubtract(a, b);
    return TMPointLength(v);
}
//点乘
CGFloat TMPointDotProduct(CGPoint a, CGPoint b){
    return a.x * b.x + a.y * b.y;
}
//叉乘
CGFloat TMPointCrossProduct(CGPoint a, CGPoint b){
    return a.x * b.y - a.y * b.x;
}
//(a,b,c)的角度
CGFloat TMPointAngle(CGPoint a, CGPoint b, CGPoint c){
    CGFloat l = TMPointLength(a) * TMPointLength(b);
    if (l > 0){
        CGPoint d = TMPointSubtract(b, a);
        CGPoint e = TMPointSubtract(c, b);
        return acos(TMPointDotProduct(d, e) / l);
    }
    return -1;
}
//模
CGFloat TMPointLength(CGPoint v){
    CGFloat l = TMPointDotProduct(v, v);
    if (l > 0){
        return sqrt(l);
    }
    return 0;
}
//(a,b)(c,d)相交于p
Boolean TMPointIntersect(CGPoint a, CGPoint b, CGPoint c, CGPoint d, CGPoint *p, CGFloat *u, CGFloat *v){
    if (TMPointEquals(a, b) || TMPointEquals(c, d)) {
        return false;
    }
    //
    CGPoint i = TMPointSubtract(b, a);
    CGPoint j = TMPointSubtract(d, c);
    CGFloat l = TMPointCrossProduct(i, j);
    if (l != 0) {
        CGPoint k = TMPointSubtract(a, c);
        CGFloat x = TMPointCrossProduct(j, k) / l;
        if (x > 1 || x < 0){
            return false;
        }
        CGFloat y = TMPointCrossProduct(i, k) / l;
        if (y > 1 || y < 0){
            return false;
        }
        if (p != NULL) {
            CGPoint t = TMPointInterpolate(a, b, x);
            p->x = t.x;
            p->y = t.y;
        }
        if (u != NULL) {
            *u = x;
        }
        if (v != NULL) {
            *v = y;
        }
        return true;
    }
    return false;
}
//p在(a,b)上的投影q
Boolean TMPointProject(CGPoint a, CGPoint b, CGPoint p, CGPoint *q){
    CGPoint c = TMPointSubtract(b, a);
    CGFloat l = TMPointDotProduct(c, c);
    if (l > 0){
        CGPoint d = TMPointSubtract(p, a);
        CGPoint t = TMPointInterpolate(a, b, TMPointDotProduct(c, d) / l);
        if (q != NULL) {
            q->x = t.x;
            q->y = t.y;
        }
        return true;
    }
    return false;
}
//等于
Boolean TMPointEquals(CGPoint a, CGPoint b){
    if (a.x != b.x || a.y != b.y){
        return false;
    }
    return true;
}
